package com.jebel.framework.classic.event;

import com.jebel.framework.classic.event.gateway.GateWayVerticle;
import com.jebel.framework.classic.event.worker.ClassicEventVerticle;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.zookeeper.ZookeeperClusterManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource(value = {"classpath:application.properties"})
public class ClassEventApplication {
    private static Logger logger = LoggerFactory.getLogger(ClassEventApplication.class);

    @Autowired
    Environment environment;

    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

        Vertx vertx = Vertx.vertx();
        DeploymentOptions option = new DeploymentOptions();
        vertx.deployVerticle(applicationContext.getBean(GateWayVerticle.class));
        vertx.deployVerticle(applicationContext.getBean(ClassicEventVerticle.class, option.setWorker(true)));

//        ClassEventApplication classEventApplication = applicationContext.getBean(ClassEventApplication.class);
//        ClusterManager clusterManager = new ZookeeperClusterManager();
//        EventBusOptions eventBusOptions = new EventBusOptions();
//        eventBusOptions.setClustered(true);
//        eventBusOptions.setClusterPublicHost(classEventApplication.getClusterPublicHost());
//        eventBusOptions.setHost(classEventApplication.getClusterHost());
//
//        VertxOptions vertxOptions = new VertxOptions();
//        vertxOptions.setClusterManager(clusterManager).setEventBusOptions(eventBusOptions);
//        Vertx.clusteredVertx(vertxOptions, res -> {
//            if (res.succeeded()) {
//                Vertx vertx = res.result();
//                DeploymentOptions option = new DeploymentOptions();
//                vertx.deployVerticle(applicationContext.getBean(GateWayVerticle.class));
//                vertx.deployVerticle(applicationContext.getBean(ClassicEventVerticle.class, option.setWorker(true)));
//                logger.info("ClassEventApplication start success.");
//            } else {
//                logger.error("ClassEventApplication start failed.");
//            }
//        });
    }

    public String getClusterPublicHost() {
        return environment.getProperty("cluster.public.host", "localhost");
    }

    public String getClusterHost() {
        return environment.getProperty("cluster.host", "localhost");
    }
}
